package com.ruoyi.security.service.impl;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.security.domain.AfElevatorInfo;
import com.ruoyi.security.elevator.util.ElevatorInterfaceUtil;
import com.ruoyi.security.elevator.util.Signed;
import com.ruoyi.security.elevator.websocket.WebSocketSessionManger;
import com.ruoyi.security.service.IAfElevatorInfoService;
import com.ruoyi.security.service.IElevatorSendService;
import com.ruoyi.system.api.enums.ElevatorValue;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.TreeMap;


@Service
@Slf4j
public class ElevatorSendServiceImpl implements IElevatorSendService {

    @Autowired
    private IAfElevatorInfoService elevatorInfoService;

    @Autowired
    private RedisService redisService;

    @Override
    public void sendGetElevatorRealData(String tag) {
        List<AfElevatorInfo> list = elevatorInfoService.selectBindElevatorInfo("1");
        String token = redisService.getCacheObject("elevator_token");

        if (StringUtils.isBlank(token)){
            // 重新获取token
        log.info("梯联网token为空");
            return;
        }

        TreeMap<String, Object> params = new TreeMap<>();
        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("token", token);
        params.put("msgType", "THIRDPARTY_START_REALTIME_DATA_REQ");
        params.put("timeStamp", ElevatorInterfaceUtil.getTimeStamp());
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("tdDataTags", tag);
        for (AfElevatorInfo info : list) {
            params.put("registerCode", info.getRegisterCode());
            params.put("regCode", info.getRegCode());
            params.put("sign", Signed.getSignString(params));

            try {
                WebSocketClient client = WebSocketSessionManger.getOnlineDevice(ElevatorValue.WEBSOCKET_URI.toString());
                if (client == null){
                    log.info("WebSocketClient is null");
                    return;
                }
                log.info("WebSocketClient send tag 1 , RegisterCode  " + info.getRegisterCode());
                client.send(JSONUtil.toJsonStr(params));
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }

    //拼装字符串用 ArrayUtil.join

    @Override
    public void sendGetElevatorRealData(String tag,WebSocketClient client) {
        log.info("sendGetElevatorRealData ");
        List<AfElevatorInfo> list = elevatorInfoService.selectBindElevatorInfo("1");
        String token = redisService.getCacheObject("elevator_token");

        if (StringUtils.isBlank(token)){
            // 重新获取token
            log.info("梯联网token为空");
            return;
        }

        TreeMap<String, Object> params = new TreeMap<>();
        params.put("appkey", ElevatorValue.APP_KEY);
        params.put("token", token);
        params.put("msgType", "THIRDPARTY_START_REALTIME_DATA_REQ");
        params.put("timeStamp", ElevatorInterfaceUtil.getTimeStamp());
        params.put("appSecret", ElevatorValue.APP_SECRET);
        params.put("tdDataTags", tag);
        for (AfElevatorInfo info : list) {
            params.put("registerCode", info.getRegisterCode());
            params.put("regCode", info.getRegCode());
            params.put("sign", Signed.getSignString(params));

            try {
                log.info("WebSocketClient send tag 1 , RegisterCode " + info.getRegisterCode());
                client.send(JSONUtil.toJsonStr(params));
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }
}
